﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.Advertisement;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms.Advertistment
{
    public partial class AdvertisementPostionManager : AdminUserControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            CHRTList<AdvertisementPositionsInfo> advertisementPositionsInfo = AdvertisementPositionsBL.SelectAll();
            if (advertisementPositionsInfo != null && advertisementPositionsInfo.Count > 0)
            {
                grdAdvertisements.DataSource = advertisementPositionsInfo;
                grdAdvertisements.DataBind();
            }
            RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {

                foreach (GridViewRow row in grdAdvertisements.Rows)
                {
                    Button btnDeleteAdv = (Button)row.FindControl("btnDeleteAdvertise");
                    btnDeleteAdv.Enabled = false;
                    btnDeleteAdv.CssClass = "Icon_DeleteOff";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void grdAdvertisements_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
                if (_roleInfo == null) return;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                AdvertisementPositionsInfo _advInfo = (AdvertisementPositionsInfo)e.Row.DataItem;
                Button btnDeleteAdvertise = (Button)e.Row.FindControl("btnDeleteAdvertise");
                btnDeleteAdvertise.Attributes.Add("onclick", MiscUtility.DeleteConfirm("nhóm quảng cáo", _advInfo.Title));
                Label lblSTT = (Label)e.Row.FindControl("lblSTT");
                lblSTT.Text = (e.Row.DataItemIndex + 1).ToString();
                Label lblSize = (Label)e.Row.FindControl("lblSize");
                lblSize.Text = "Rộng: <b><font color='blue'>" + _advInfo.Width + "</font></b> px, Cao: <b><font color='blue'>" + _advInfo.Height + "</font></b> px";
                Label lblCreatedOn = (Label)e.Row.FindControl("lblCreatedOn");
                lblCreatedOn.Text = MiscUtility.GetLongDateTime(_advInfo.CreatedOn);
                Label lblCreatedBy = (Label)e.Row.FindControl("lblCreatedBy");
                UserInfo _userInfo = UserController.GetUser(CurrentUser.UserID);
                if (_userInfo != null)
                {
                    lblCreatedBy.Text = _userInfo.Username;
                }
                Label lblPosition = (Label)e.Row.FindControl("lblPosition");

                BannerPos bp = BannerPos.GetPos(_advInfo.PositionID);
                if (bp != null)
                {
                    lblPosition.Text = bp.PosName;
                }
            }
        }

        protected void grdAdvertisements_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int advId = ObjConvertUtility.ToInt32(e.CommandArgument);

            try
            {
                switch (e.CommandName)
                {
                    case "deleteadvertise":
                        if (advId == 0) return;
                        //xóa danh sach banner
                        AdvertisementBL.Delete(0, advId);
                        //xoa nhom
                        AdvertisementPositionsBL.Delete(advId);
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void grdAdvertisements_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAdvertisements.PageIndex = e.NewPageIndex;
        }
    }
}